﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

using GarageGames.Torque.Core;
using GarageGames.Torque.Util;
using GarageGames.Torque.Sim;
using GarageGames.Torque.T3D;
using GarageGames.Torque.SceneGraph;
using GarageGames.Torque.MathUtil;
using GarageGames.Torque.T3D.RigidCollision;
using GarageGames.Torque.Materials;

namespace TimeWar
{

    public class ProjectileTemplate : TorqueObject, ITickObject
    {
        private float _timer = 300f;

        public string TextureFilename
        {
            get
            {
                T3DSphere sphereComponent = Components.FindComponent<T3DSphere>();
                return sphereComponent.Material.Name;
            }
            set
            {
                T3DSphere sphereComponent = Components.FindComponent<T3DSphere>();
                sphereComponent.Material.Name = value;                
            }
        }

        public float Timer
        {
            get { return _timer; }
            set { _timer = value; }
        }

        public ProjectileTemplate()
        {
            //find the physics resolver
            RigidCollisionManager rigidManager = TorqueObjectDatabase.Instance.FindObject<RigidCollisionManager>("RigidManager");

            Name = "ProjectileTemplate";
            IsTemplate = true;
            
            T3DSceneComponent componentScene = new T3DSceneComponent();
            componentScene.SceneGroup = "ProjectileTemplate";
            Components.AddComponent(componentScene);

            SimpleMaterial material = new SimpleMaterial();
            material.TextureFilename = @"data\Weapons\tracer.png";

            T3DSphere sphereComponent = new T3DSphere();
            sphereComponent.SceneGroupName = "ProjectileTemplate";
            sphereComponent.Material = material;
            sphereComponent.Radius = 0.05f;
            Components.AddComponent(sphereComponent);

            //define a proper collision shape
            CollisionSphereShape collisionShape = new CollisionSphereShape();
            collisionShape.Radius = 0.65f;
            collisionShape.Center = new Vector3(0.0f, -0.2f, 0.65f);

            //give the player some physics attributes, like gravity and collision
            T3DRigidComponent componentPhysics = new T3DRigidComponent();
            componentPhysics.SceneGroupName = "ProjectileTemplate";
            componentPhysics.RigidManager = rigidManager;
            componentPhysics.GravityScale = 0;
            componentPhysics.CollisionBody.AddCollisionShape(collisionShape);
            Components.AddComponent(componentPhysics);
            
            //give the player some physics attributes, like gravity and collision
            Components.AddComponent(componentPhysics);

        }

        public virtual void InterpolateTick(float dt)
        {

        }

        public virtual void ProcessTick(Move move, float dt)
        {
            T3DSceneComponent sceneComponent = Components.FindComponent<T3DSceneComponent>();
            Vector3 currPosition = sceneComponent.Position;
            T3DRigidComponent rigidComponent = Components.FindComponent<T3DRigidComponent>();
            Vector3 heading = rigidComponent.Velocity;
            Vector3.Add(currPosition, heading * dt);
            sceneComponent.Position = currPosition;            
        }

        public override void CopyTo(TorqueObject obj)
        {
            base.CopyTo(obj);
            ProjectileTemplate obj2 = obj as ProjectileTemplate;
            obj2.TextureFilename = TextureFilename;
            obj2.Timer = _timer;
        }


    }
}
